<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
namespace addon\mall\logic;
use app\common\logic\LogicBase;
use \tpfcore\Core;
use think\Db;
use \tpfcore\helpers\StringHelper;

class Order extends LogicBase
{
	public function getOrder($data=[]){
		return self::getList($data);
	}
	/*
		创建订单逻辑
		1、生成订单
		2、加入订单产品（暂时没有此功能）
		3、清空购物车（暂时没有此功能）
	*/
	public function createOrder($data=[]){
		// 设置 goods_price  商品总价
        if(!isset($data['goods_price'])){
        	$data['goods_price'] = $data['order_amount'];
        }
        // 设置 total_amount  订单总价
        if(!isset($data['total_amount'])){
        	$data['total_amount'] = $data['order_amount'];
        }
		// 订单数据验证
		$validate=Core::addonValidate($this->name);
        $validate_result = $validate->scene("add")->check($data);
        if (!$validate_result) {
            return [40048, $validate->getError(), null];
        }
        // 设置 order_note  订单信息
        if(!isset($data['order_note'])){
        	$data['order_note'] = "订单支付";
        }
        // 设置 支付code
        if(!isset($data['pay_code'])){
	        if(request()->isMobile()){
	        	$data['pay_code'] = "wap";
	        }else{
	        	$data['pay_code'] = "pc";
	        }
        }


        // 设置 支付名称
        if(!empty($data['pay_handle'])){
        	switch ($data['pay_handle']) {
	        	case 'alipay':
	        		$data['pay_name']="支付宝";
	        		break;
	        	case 'weixin':
	        		$data['pay_name']=$data['pay_code']=="wxapp"?"微信小程序":"微信";
	        		break;
	        	default:
	        		$data['pay_name']="其它";
	        		break;
	        }
        }

		//  需要验证的数据  goods_price  order_amount
		$order_sn=StringHelper::get_order_sn("SN");
		$data['order_sn']=$order_sn;
		Db::startTrans();
		try{
			$last_id = Core::loadAddonModel($this->name)->saveObject($data,['order_sn','order_note','seller_id','user_id','address','zipcode','goods_price','order_amount','total_amount','pay_handle','pay_name','pay_code','shipping_price','user_money','user_note','add_time','consignee','mobile']);
			if($last_id){
				$order_goods = $data['order_goods'];
				$validate=Core::addonValidate("OrderGoods");
				foreach ($order_goods as $key => $order_good) {
					unset($order_good['id']);
					// 设置 goods_price  商品总价
			        if(!isset($order_good['market_price'])){
			        	$order_good['market_price'] = $order_good['goods_price'];
			        }
					$order_good['order_id']=$last_id;
			        $validate_result = $validate->scene("add")->check($order_good);
			        if (!$validate_result) {
			            throw new \Exception($validate->getError());
			        }
			        if(is_object($order_good)) $order_good=$order_good->toArray();
			        $result = Core::loadAddonModel("OrderGoods")->saveObject($order_good);
			        if(!$result){
			        	throw new \Exception($validate->getError());
			        }
				}

				$data_order_action['order_id']=$last_id;
				$data_order_action['user_id']=empty($data['user_id'])?0:$data['user_id'];
				$data_order_action['note']="确认订单";
				$data_order_action['desc']="用户确认订单";

				$result = Core::loadAddonModel("OrderAction")->saveObject($data_order_action);
				if(!$result){
		        	throw new \Exception($validate->getError());
		        }

				Db::commit(); 
				return [0,"订单生成成功",["order_sn"=>$order_sn,"pay_url"=>url('payment/order/pay',['order_sn'=>$order_sn])]];
			}else{
				return [40048,"订单生成失败"];
			}
		}catch(\Exception $e){
			Db::rollback();
			return [40048,"订单生成失败".$e->getMessage()];
		}
	}

	public function updateOrder($where=[],$data=[]){
		$result = self::updateObject($where,$data);
		if($result){
			return true;
		}
		return false;
	}
}